package xiaohongshu

import (
	"marketing-server/global"
	"marketing-server/model/marketing/xiaohongshu"
	xiaohongshuReq "marketing-server/model/marketing/xiaohongshu/request"
)

type XhsPreAuthCodeService struct{}

// CreateXhsPreAuthCode 创建xhsPreAuthCode表记录
// Author [yourname](https://github.com/yourname)
func (xhsPreAuthCodeService *XhsPreAuthCodeService) CreateXhsPreAuthCode(xhsPreAuthCode *xiaohongshu.XhsPreAuthCode) (err error) {
	err = global.MustGetGlobalDBByDBName("xiaohongshuAlias").Create(xhsPreAuthCode).Error
	return err
}

// DeleteXhsPreAuthCode 删除xhsPreAuthCode表记录
// Author [yourname](https://github.com/yourname)
func (xhsPreAuthCodeService *XhsPreAuthCodeService) DeleteXhsPreAuthCode(id string) (err error) {
	err = global.MustGetGlobalDBByDBName("xiaohongshuAlias").Delete(&xiaohongshu.XhsPreAuthCode{}, "id = ?", id).Error
	return err
}

// DeleteXhsPreAuthCodeByIds 批量删除xhsPreAuthCode表记录
// Author [yourname](https://github.com/yourname)
func (xhsPreAuthCodeService *XhsPreAuthCodeService) DeleteXhsPreAuthCodeByIds(ids []string) (err error) {
	err = global.MustGetGlobalDBByDBName("xiaohongshuAlias").Delete(&[]xiaohongshu.XhsPreAuthCode{}, "id in ?", ids).Error
	return err
}

// UpdateXhsPreAuthCode 更新xhsPreAuthCode表记录
// Author [yourname](https://github.com/yourname)
func (xhsPreAuthCodeService *XhsPreAuthCodeService) UpdateXhsPreAuthCode(xhsPreAuthCode xiaohongshu.XhsPreAuthCode) (err error) {
	err = global.MustGetGlobalDBByDBName("xiaohongshuAlias").Model(&xiaohongshu.XhsPreAuthCode{}).Where("id = ?", xhsPreAuthCode.Id).Updates(&xhsPreAuthCode).Error
	return err
}

// GetXhsPreAuthCode 根据id获取xhsPreAuthCode表记录
// Author [yourname](https://github.com/yourname)
func (xhsPreAuthCodeService *XhsPreAuthCodeService) GetXhsPreAuthCode(id string) (xhsPreAuthCode xiaohongshu.XhsPreAuthCode, err error) {
	err = global.MustGetGlobalDBByDBName("xiaohongshuAlias").Where("id = ?", id).First(&xhsPreAuthCode).Error
	return
}

// GetXhsPreAuthCodeInfoList 分页获取xhsPreAuthCode表记录
// Author [yourname](https://github.com/yourname)
func (xhsPreAuthCodeService *XhsPreAuthCodeService) GetXhsPreAuthCodeInfoList(info xiaohongshuReq.XhsPreAuthCodeSearch) (list []xiaohongshu.XhsPreAuthCode, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.MustGetGlobalDBByDBName("xiaohongshuAlias").Model(&xiaohongshu.XhsPreAuthCode{})
	var xhsPreAuthCodes []xiaohongshu.XhsPreAuthCode
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&xhsPreAuthCodes).Error
	return xhsPreAuthCodes, total, err
}
func (xhsPreAuthCodeService *XhsPreAuthCodeService) GetXhsPreAuthCodePublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
